import Vue from "vue";
import Vuex from "vuex";
import common from "./modules/common.js"
Vue.use(Vuex);
import {
	userInfo
} from "../utils/api";
const store = new Vuex.Store({
	namespaced: true,
	modules: {
		common
	},
	state: {
		/**
		 * 要编辑的地址
		 * id
		 * name 联系人名称
		 * city 收货地址
		 * mobile 联系方式
		 */
		eidtAddressInfo: {
			id: "",
			name: "",
			city: "",
			mobile: "",
		},
		/**
		 * 用户个人信息
		 */
		userInfo: uni.getStorageSync('userInfo') || {
			name: "用户名",
			headavatar: "",
			sex: "",
			auth_name: "",
			mobile: "",
			city: ''
		},
		/**
		 * 选择下单地址
		 */
		orderAddress: null,
		/**
		 * 暂存购买的商品
		 */
		goods: [],
		/**
		 * 小程序分享推荐用户id
		 */
		shareId: "",
		/**
		 * 下单的购物车
		 */
		bagsRecord: [],
		/**
		 * 分享推荐下单
		 */
		remomandGoods: {},
		/**
		 * 开通会员类型
		 */
		vipType: 0,
		/**
		 * 城市股东
		 */
		city: {},
	},
	getters: {
		eidtAddressInfo: (state) => state.eidtAddressInfo,
		userInfo: (state) => state.userInfo,
		orderAddress: (state) => state.orderAddress,
		goods: (state) => state.goods,
		shareId: (state) => state.shareId,
		bagsRecord: (state) => state.bagsRecord,
		remomandGoods: (state) => state.remomandGoods,
		vipType: (state) => state.vipType,
		city: (state) => state.city,
		isVip: (state) => {
			let flag = false
			if (!state.userInfo) return flag
			// VIP会员逻辑
			let endTime = new Date(state.userInfo.vip_expiration_time * 1000).getTime()
			flag = endTime > new Date().getTime()
			if (flag) return flag
			// 合伙人逻辑
			flag = state.userInfo.is_partner === 1
			return flag
		}
	},
	mutations: {
		SET_EDITADDRESS: (state, addressObj) => {
			for (var key in addressObj) {
				state.eidtAddressInfo[key] = addressObj[key];
			}
		},
		SET_USERINFO: (state, userInfo) => {
			// for (var key in userInfo) {
			//   state.userInfo[key] = userInfo[key];
			// }
			state.userInfo = userInfo;
		},
		// SET_AVATAR:(state,avatar)=>{
		//   state.userInfo.headavatar = avatar;
		// },
		// SET_SEX:(state,sex)=>{
		//   state.userInfo.sex = sex;
		// },
		// SET_SEX:(state,sex)=>{
		//   state.userInfo.sex = sex;
		// },
		SET_ATTR(state, obj) {
			state.userInfo[obj["key"]] = obj["value"];
		},
		SET_ORDERADDRESS(state, obj) {
			state.orderAddress = obj;
		},
		SET_GOODS: (state, goodsArr) => {
			state.goods = [...goodsArr];
		},
		CLEAR_GOODS: (state) => {
			state.goods = [];
		},
		SET_SHAREID: (state, shareId) => {
			state.shareId = shareId;
		},
		SET_BAGSRECORD: (state, bagsRecord) => {
			state.bagsRecord = [...bagsRecord];
		},
		// remomandGoods
		SET_RECOMANDGOODS: (state, recoObj) => {
			state.remomandGoods = recoObj;
		},
		// vipType
		SET_VIPTYPE: (state, vipType) => {
			state.vipType = vipType;
		},
		SET_CITY: (state, city) => {
			state.city = city;
		},
	},
	actions: {
		// 记录编辑的地址
		recordEditAddress({
			commit
		}, addressObj) {
			return new Promise((resolve) => {
				commit("SET_EDITADDRESS", addressObj);
				resolve();
			});
		},
		// 清掉用户信息
		clearUserInfo({
			commit
		}) {
			return new Promise((resolve) => {
				commit("SET_USERINFO", {});
				resolve();
			});
		},
		// 设置用户个人信息
		setUserInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				userInfo().then((resp) => {
					if (resp.code == 200) {
						console.log("用户个人信息", resp);
						commit("SET_USERINFO", resp.data);
						resolve();
					} else {
						reject();
					}
				});
			});
		},
		setAttr({
			commit
		}, obj) {
			return new Promise((resolve) => {
				commit("SET_ATTR", obj);
				uni.showToast({
					icon: "none",
					title: "修改成功",
				});
				resolve();
			});
		},
		setOrderAddress({
			commit
		}, obj) {
			return new Promise((resolve, reject) => {
				commit("SET_ORDERADDRESS", obj);
				resolve();
			});
		},
		saveGoodsTemporary({
			commit
		}, goodsArr) {
			return new Promise((resolve) => {
				commit("SET_GOODS", goodsArr);
				resolve();
			});
		},
		// 清除缓存的要下单的商品
		clearGoods({
			commit
		}) {
			return new Promise((resolve) => {
				commit("CLEAR_GOODS");
				resolve();
			});
		},
		saveShareId({
			commit
		}, shareId) {
			return new Promise((resolve) => {
				commit("SET_SHAREID", shareId);
				resolve();
			});
		},
		saveBagsRecord({
			commit
		}, bagsRecord) {
			return new Promise((resolve) => {
				commit("SET_BAGSRECORD", bagsRecord);
				resolve();
			});
		},
		saveRecomandGoods({
			commit
		}, recoGoods) {
			return new Promise((resolve) => {
				commit("SET_RECOMANDGOODS", recoGoods);
				resolve();
			});
		},
		saveVipType({
			commit
		}, vipType) {
			return new Promise((resolve) => {
				commit("SET_VIPTYPE", vipType);
				resolve();
			});
		},
		saveCity({
			commit
		}, city) {
			return new Promise((resolve) => {
				commit("SET_CITY", city);
				resolve();
			});
		},
	},
});
export default store;
